package com.ziven.common.widget.setting;

import android.view.View;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;

import com.ziven.common.widget.R;
import com.ziven.common.widget.value.Background;
import com.ziven.common.widget.value.NullValue;
import com.ziven.common.widget.value.Value;

import java.util.List;

/**
 * @author Ziven
 */
public interface PreferenceItemsFactory {

    /**
     * Create BasePreferenceItem of list
     * @return List<BasePreferenceItem>
     */
    @NonNull
    List<BasePreferenceItem> getPreferenceItems();

    /**
     * Get Layout id
     * @param type Type
     * @return Layout id
     */
    @LayoutRes
    default int getPreferenceItemLayoutId(int type) {
        if(type == LinePreferenceItem.TYPE_LINE) {
            return R.layout.item_setting_line;
        }
        if(type == TitlePreferenceItem.TYPE_TITLE) {
            return R.layout.item_setting_title;
        }
        return R.layout.item_setting_container;
    }

    /**
     * Get preference iterm Effect
     * @param type Type
     * @return Value Array
     */
    @NonNull
    default Value getPreferenceItemEffect(int type) {
        if(type == PreferenceFragment.TYP_BACKGROUND) {
            return Background.colorId(R.color.setting_page_background_color);
        }
        return NullValue.VALUE;
    }

    /**
     * Bind Layout
     * @param holder PreferenceHolder
     * @param data BasePreferenceItem
     * @param position Position
     */
    default void bindPreferenceItemLayout(@NonNull PreferenceHolder holder,
                                          @NonNull BasePreferenceItem data,
                                          int position) {

        data.effect().takeEffect(holder.itemView);

        if(data.getViewType() == LinePreferenceItem.TYPE_LINE) {
            return;
        }

        if(data.getViewType() == TitlePreferenceItem.TYPE_TITLE) {
            data.takeEffect(holder.getViewHelper(R.id.item_title_container), data.getTitle());
            return;
        }

        data.takeEffect(holder.getViewHelper(R.id.title), data.getTitle());
        data.takeEffect(holder.getViewHelper(R.id.icon), data.getIcon());
        data.takeEffect(holder.getViewHelper(R.id.sub_icon), data.getSubIcon());
        data.takeEffect(holder.getViewHelper(R.id.sub_title_1), data.getSubTitle1());
        data.takeEffect(holder.getViewHelper(R.id.sub_title_2), data.getSubTitle2());
    }

    /**
     * Get OnItemClickListener
     * @return OnItemClickListener
     */
    @NonNull
    default OnPreferenceItemClickListener getPreferenceItemClickListener() {
        return (data, position, view) -> {
            if(onPreferenceItemClick(data, position, view)) {
                return true;
            }
            return data.onItemClick(data, position, view);
        };
    }

    /**
     * onPreferenceItemClick
     * @param data BasePreferenceItem
     * @param position Position
     * @param view View
     * @return return
     */
    default boolean onPreferenceItemClick(BasePreferenceItem data,
                                          int position,
                                          @NonNull View view) {
        return false;
    }

    /**
     * Set CallGetPreferenceItems
     * @param call {@link CallGetPreferenceItems}
     */
    default void setCallGetPreferenceItems(CallGetPreferenceItems call) {}

    interface CallGetPreferenceItems {
        /**
         * Call {@link PreferenceItemsFactory#getPreferenceItems()}
         */
        void call();
    }
}
